package com.bzxgc.sgwer.service.impl.user;

import com.bzxgc.sgwer.common.util.PageData;
import com.bzxgc.sgwer.dao.user.UserMapper;
import com.bzxgc.sgwer.entity.user.User;
import com.bzxgc.sgwer.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserByLoginName(String loginName) {
        return userMapper.getUserByLoginName(loginName);
    }

    @Override
    public PageData<User> getUserListByPage(Integer pageNum, Integer pageSize, String name) {
        List<User> userList = userMapper.getUserListByPage(PageData.fromIndex(pageNum, pageSize), pageSize, name);
        Integer totalRecord = userMapper.getUserCount(name);
        PageData<User> pageData = new PageData<>(pageSize, pageNum, totalRecord, userList);
        return pageData;
    }

    @Override
    public Integer getUserCount(String name) {
        return userMapper.getUserCount(name);
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.getUserById(id);
    }

    @Override
    public void addUser(User user) {
        user.setAddTime(new Date());
        user.setStatus(1);
        user.setPassword("666666");
        userMapper.addUser(user);
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(new Date());
        userMapper.updateUser(user);
    }

    @Override
    public void updateUserStatus(Long[] ids, Integer status) {
        userMapper.updateUserStatus(ids, status);
    }
}
